import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/dropdownmenu.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//盘库作业
class InventoryOperation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InventoryOperation();
  }
}

//ListWidget
class _InventoryOperation extends State<InventoryOperation> {
  List<dynamic> dropdownList = new List();
  bool load = true;
  String selectValue;
  String selectlibrary;
  List<dynamic> libraryData = new List();
  Color loadBgColor = Colors.white;


  @override
  void initState() {
    super.initState();
    getLibrary();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "盘库作业",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(bgColor: loadBgColor,visibility: load,builder: Column(
          children: <Widget>[
            Expanded(
                child: Container(
                  padding: EdgeInsets.only(top: 20, left: 12, right: 12),
                  child: Column(
                    children: <Widget>[
                      DropDownMenu(
                        value: selectValue,
                        datas: ["全仓盘库","指定库区盘库"],
                        hitText: "选择盘库目标",
                        onChange: (data){
                          if(data["value"]=="全仓盘库"){
                            setState(() {
                              selectValue=data["value"];
                              libraryData=[];
                            });
                          }else{
                            setState(() {
                              selectValue=data["value"];
                              libraryData=dropdownList;
                            });
                          }
                        },
                      ),
                      DropDownMenu(
                        value: selectlibrary,
                        datas: libraryData,
                        keyItem: {"id":"text"},
                        hitText: "选择盘库库区",
                        onChange: (data){
                          setState(() {
                            selectlibrary=data["value"];
                          });

                        },
                      ),
                    ],
                  ),
                )),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                    padding: EdgeInsets.all(15),
                    color: Color(0xFF42AFF0),
                    child: Text(
                      "提交",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      if(selectValue!=null){
                        setState(() {
                          load=true;
                          loadBgColor = Color(0x66BCBCBC);
                        });
                        addMiWarehouseCheck();
                      }else{
                        TostUtil.showToast("请选择盘库目标");
                      }
                    },
                  ),
                )
              ],
            )
          ],
        ),)

    );
  }
  void getLibrary() {
    InternetAPI.getInstance().get("Facilities/PrWarehouseArea/GetAreaTree",
            (data) {
          print(data);
          setState(() {
            dropdownList = data["dropdownList"];
            load = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'pageNo': 1,
          'pageSize': 400
        });
  }

  void addMiWarehouseCheck() {
    InternetAPI.getInstance().post("WMS/MiWarehouseCheck/AddMiWarehouseCheck",
            (data) {
          print(data);
          setState(() {
            load = false;
          });
          Navigator.of(context).pushNamed("/inventoryopr_list",arguments: data);
        },errorCallBack: (err){
          setState(() {
            load = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
        'Areaid': selectlibrary,
        'checktargetid': selectValue=="全仓盘库"?1:2,

        });

  }
}
